﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace UpdateSql
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string conStr = string.Empty;
        private bool connected = false;
        private void btn_Connect_Click(object sender, EventArgs e)
        {
            if (btn_Connect.Text.Equals("连接"))
            {
                conStr = "Data Source=" + txt_IP.Text + ";Initial Catalog=master;User Id=" + txt_User.Text + "; Password=" + txt_Pwd.Text + ";";
                string sql = "select * from sysdatabases";
                try
                {
                    cmb_DataBase.DataSource = DBHelper.GetSet(sql, conStr, CommandType.Text).Tables[0];
                    cmb_DataBase.ValueMember = "dbid";
                    cmb_DataBase.DisplayMember = "name";
                    cmb_DataBase.Enabled = true;
                    txt_IP.Enabled = false;
                    txt_User.Enabled = false;
                    txt_Pwd.Enabled = false;
                    btn_Connect.Text = "关闭连接";
                    connected = true;
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }

            }
            else
            {
                connected = false;
                btn_Connect.Text = "连接";

                cmb_DataBase.DataSource = null;
                cmb_View.DataSource = null;
                cmb_Proc.DataSource = null;

                cmb_DataBase.Enabled = false;
                cmb_Proc.Enabled = false;
                cmb_View.Enabled = false;

                txt_IP.Enabled = true;
                txt_User.Enabled = true;
                txt_Pwd.Enabled = true;

                txt_Instrection.Text = string.Empty;

            }

        }

        private void cmb_DataBase_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string sql = "use " + cmb_DataBase.Text + " select name from sysobjects where type='P'";
                cmb_Proc.DataSource = null;
                cmb_Proc.DataSource = DBHelper.GetSet(sql, conStr, CommandType.Text).Tables[0];
                cmb_Proc.DisplayMember = "name";
                cmb_Proc.Enabled = true;
            }
            catch (Exception err)
            {
                if (connected)
                {
                    MessageBox.Show(err.Message);
                }
            }
            try
            {
                string sql = "use " + cmb_DataBase.Text + " select * from sysobjects where xtype='V'";
                cmb_View.DataSource = null;
                cmb_View.DataSource = DBHelper.GetSet(sql, conStr, CommandType.Text).Tables[0];
                cmb_View.DisplayMember = "name";
                cmb_View.ValueMember = "id";
                cmb_View.Enabled = true;

            }
            catch (Exception err)
            {
                if (connected)
                {
                    MessageBox.Show(err.Message);
                }
            }
        }

        private void btn_View_Click(object sender, EventArgs e)
        {
            txt_Instrection.Clear();
            try
            {
                string sql = "use " + cmb_DataBase.Text + " exec sp_helptext @objname";
                DataTable dt = DBHelper.GetSet(sql, conStr, CommandType.Text, new SqlParameter("@objname", cmb_View.Text)).Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    txt_Instrection.AppendText(dt.Rows[i]["Text"].ToString());
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void btn_Proc_Click(object sender, EventArgs e)
        {
            txt_Instrection.Clear();
            try
            {
                string sql = "use " + cmb_DataBase.Text + " exec sp_helptext @objname";
                DataTable dt = DBHelper.GetSet(sql, conStr, CommandType.Text, new SqlParameter("@objname", cmb_Proc.Text)).Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    txt_Instrection.AppendText(dt.Rows[i]["Text"].ToString());
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void cmb_View_EnabledChanged(object sender, EventArgs e)
        {
            btn_View.Enabled = cmb_View.Enabled;
        }

        private void cmb_Proc_EnabledChanged(object sender, EventArgs e)
        {
            btn_Proc.Enabled = cmb_Proc.Enabled;
        }

        private void txt_Instrection_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txt_Instrection.Text) && !connected)
            {
                if (btn_Run.Enabled)
                {
                    btn_Run.Enabled = false;
                    cmb_CommandType.Enabled = false;
                }
            }
            else
            {
                if (!btn_Run.Enabled)
                {
                    btn_Run.Enabled = true;
                    cmb_CommandType.Enabled = true;
                }
            }
        }

        private void btn_Run_Click(object sender, EventArgs e)
        {
            try
            {
                conStr = "Data Source=" + txt_IP.Text + ";Initial Catalog=" + cmb_DataBase.Text + ";User Id=" + txt_User.Text + "; Password=" + txt_Pwd.Text + ";";

                CommandType ct = CommandType.Text;
                switch (cmb_CommandType.Text)
                {
                    case "文本":
                        ct = CommandType.Text;
                        break;
                    case "存储过程":
                        ct = CommandType.StoredProcedure;
                        break;
                    case "表":
                        ct = CommandType.TableDirect;
                        break;
                }

                MessageBox.Show(DBHelper.GetNonQuery(txt_Instrection.Text, conStr, ct) == -1 ? "成功" : "失败");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            cmb_CommandType.SelectedText = "文本";
            string name = Dns.GetHostName();
            IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
            foreach (IPAddress ipa in ipadrlist)
            {
                if (ipa.AddressFamily == AddressFamily.InterNetwork)
                {
                    txt_IP.Text = ipa.ToString().Remove(ipa.ToString().LastIndexOf(".")) + ".";
                }
            }
        }
    }
}
